/**
 * Created by Administrator on 2017/10/20.
 */


//tween函数
var Tween = {
    bounceIn: function(t, b, c, d) { //弹球减振（弹球渐出）
        return c - Tween['bounceOut'](d - t, 0, c, d) + b;
    },
    bounceOut: function(t, b, c, d) {
        if ((t /= d) < (1 / 2.75)) {
            return c * (7.5625 * t * t) + b;
        } else if (t < (2 / 2.75)) {
            return c * (7.5625 * (t -= (1.5 / 2.75)) * t + 0.75) + b;
        } else if (t < (2.5 / 2.75)) {
            return c * (7.5625 * (t -= (2.25 / 2.75)) * t + 0.9375) + b;
        }
        return c * (7.5625 * (t -= (2.625 / 2.75)) * t + 0.984375) + b;
    },
    bounceBoth: function(t, b, c, d) {
        if (t < d / 2) {
            return Tween['bounceIn'](t * 2, 0, c, d) * 0.5 + b;
        }
        return Tween['bounceOut'](t * 2 - d, 0, c, d) * 0.5 + c * 0.5 + b;
    }
};
var pendant = document.getElementById('pendant') || document.getElementsByClassName('i-pendant')[0],
    posX = parseInt(pendant.offsetLeft),
    posY = parseInt(pendant.offsetTop),
    pW = pendant.clientWidth,
    pH = pendant.clientHeight,
    screenWidth = document.documentElement.clientWidth,
    screenHeight = document.documentElement.clientHeight,
    isBounce = false,
    innerW,
    innerH,
    moveX,
    moveY,
    mX,
    mY;
//        按下
pendant.addEventListener('touchstart', function(event) {
    if (isBounce) {
        return false;
    }
    posX = parseInt(pendant.offsetLeft);
    posY = parseInt(pendant.offsetTop);
    var _touchs = event.touches[0],
        ofX = _touchs.clientX,
        ofY = _touchs.clientY;
    innerW = ofX - posX;
    innerH = ofY - posY;
});
//        移动
pendant.addEventListener('touchmove', function(event) {
    if (isBounce) {
        return false;
    }
    event.preventDefault();
    var _touchs = event.touches[0];
    moveX = _touchs.clientX;
    moveY = _touchs.clientY;
    //            mX = moveX - innerW;
    mX = (moveX - innerW) > (screenWidth - pW) ? (screenWidth - pW) : ((moveX - innerW) > 0 ? (moveX - innerW) : 0);
    mY = (moveY - innerH) > (screenHeight - pH) ? (screenHeight - pH) : ((moveY - innerH) > 0 ? (moveY - innerH) : 0);
    pendant.style.left = mX + 'px';
    pendant.style.top = mY + 'px';
});
//        松开
document.addEventListener('touchend', function(event) {
    if (isBounce) {
        return false;
    }
    isBounce = true;
    posX = parseInt(pendant.offsetLeft);
    if (posX + (pW / 2) >= screenWidth / 2) {
        //            靠右
        isL = false;
        setTimeout(function() {
            bounce();
        }, 200);
    } else {
        //            靠左
        isL = true;
        setTimeout(function() {
            bounce();
        }, 200);
    }
});
var t, b, c, d, isL = false;
//        弹性动画
function bounce() {
    t = 0;
    b = posX;
    c = (isL ? 0 : screenWidth - pW) - posX;
    d = 50;

    function sport() {
        t += 1;
        if (t == d) {
            clearInterval(timer);
            isBounce = false;
        }
        pendant.style.left = Tween.bounceOut(t, b, c, d) + 'px';
    }
    var timer = setInterval(sport, 10);
}
